package applications.libraryForm.fc.lib;

import java.util.Observable;

import ontocompo.api.OntoCompoIntrospection;

import org.objectweb.fractal.fraclet.annotations.Component;
import org.objectweb.fractal.fraclet.annotations.Interface;

import applications.libraryForm.fc.api.Address;
import applications.libraryForm.fc.api.Author;
import applications.libraryForm.fc.api.Book;
import applications.libraryForm.fc.api.BorrowedBook;
import applications.libraryForm.fc.api.CodeBook;
import applications.libraryForm.fc.api.LibraryForm;
import applications.libraryForm.fc.api.LibraryFormDirectoryService;
import applications.studentApplicationForm.fc.api.AcademicRecord;
import applications.studentApplicationForm.fc.api.StudentForm;
import applications.studentApplicationForm.fc.lib.AddressImpl;

@Component(provides = {@Interface(name = "libraryformdirectoryservice", signature = applications.libraryForm.fc.api.LibraryFormDirectoryService.class), @Interface(name = "observable", signature = ontocompo.api.Observable.class), @Interface(name = "ontocompointrospection", signature = ontocompo.api.OntoCompoIntrospection.class)})

public class LibraryFormDirectoryServiceImpl extends Observable implements LibraryFormDirectoryService,ontocompo.api.Observable, OntoCompoIntrospection {


	private String studentNumber;
	private String uid;
	private org.objectweb.fractal.api.Component c;


	private LibraryForm[] librarys;

	public LibraryFormDirectoryServiceImpl() {
		super();
		this.uid = "applications.libraryForm.fc.lib.LibraryFormDirectoryServiceImpl";

		librarys = new LibraryForm[2];
		librarys[0] =  buildFirstLibrary();
		librarys[1] =  buildSecondLibrary();
	}


	@Override
	public LibraryForm getLibraryForm() {

		if (studentNumber.equals("12345"))
		{
			return librarys[0];
		}
		else
		{
			if (studentNumber.equals("56789")) 
			{

				return librarys[1];
			}
			else
			{
				return null;
			}
		}
	}

	@Override
	public void setStudentNumber(String studentNumber) {
		// TODO Auto-generated method stub
		this.studentNumber = studentNumber;
		setChanged();
		notifyObservers();

	}

	public void clearChanged() {
		super.clearChanged();
	}

	public void setChanged() {
		super.setChanged();
	}

	@Override
	public org.objectweb.fractal.api.Component getFractalComponent() {
		// TODO Auto-generated method stub
		return this.c;
	}

	@Override
	public String getUid() {
		// TODO Auto-generated method stub
		return this.uid;
	}

	@Override
	public void setFractalComponent(org.objectweb.fractal.api.Component c) {
		// TODO Auto-generated method stub
		this.c = c;
	}

	@Override
	public void setUid(String uid) {
		// TODO Auto-generated method stub
		this.uid = uid;
	}


	private LibraryForm buildSecondLibrary() {


		// les auteurs

		Author author1 = new AuthorImpl("Timonthy", "Fisher");
		Author author2 = new AuthorImpl("Stanley", "Lippman");

		Author author3= new AuthorImpl("Jean", "Piel");
		Author author4= new AuthorImpl("David", "Chles");



		// code pour les livres

		CodeBook code005 = new CodeBookImpl("005", "Informatique");
		CodeBook code33 = new CodeBookImpl("33", "Science Politique");


		CodeBook code34 = new CodeBookImpl("34", "Droit");
		CodeBook code311 = new CodeBookImpl("311", "Statistiques");

		CodeBook code51 = new CodeBookImpl("51", "Mathematiques");
		CodeBook code53 = new CodeBookImpl("53", "Physique");

		CodeBook code54 = new CodeBookImpl("54", "Chimie");
		
		CodeBook code57 = new CodeBookImpl("57", "Anglais");






		// les livres

		Book book1 = new BookImpl("111", "JAVA",author1,6,code005);
		Book book2 = new BookImpl("222", "C++", author2, 2, code005);

		Book book3 = new BookImpl("333", "Droit", author1,3,code34);
		Book book4 = new BookImpl("444", "nature de la physique", author4,2,code53);


		// les Emprunts


		BorrowedBook borrowedBook1 = new BorrowedBookImpl("01", "11/01/2011", "21/01/2011","21/01/2011", "Returned", book1);
		BorrowedBook borrowedBook2 = new BorrowedBookImpl("02", "12/01/2011", "24/01/2011","24/01/2011", "Returned", book2);

		BorrowedBook borrowedBook3 = new BorrowedBookImpl("03", "13/01/2011", "30/01/2011","", "No Returned", book3);
		BorrowedBook borrowedBook4 = new BorrowedBookImpl("04", "14/01/2011", "24/02/2011", "", "No Returned", book4);



		// adresse
		Address adress = new applications.libraryForm.fc.lib.AddressImpl("3 rue Jardin", "06000", "France", "0612345678");


		BorrowedBook[] borrowedBooks = new BorrowedBook[]{borrowedBook1,borrowedBook2,borrowedBook3,borrowedBook4};


		LibraryForm libraryForm = new LibraryFormImpl("Charles", "Dupond", "12/09/1990", "Student",
				"30/06/2014", "UFR SCIENCES", "12345", "23909766", adress, borrowedBooks);



		return libraryForm;


	}


	private LibraryForm buildFirstLibrary() {
		// TODO Auto-generated method stub


		// les auteurs

		Author author1 = new AuthorImpl("Theoneste", "Fisher");
		Author author2 = new AuthorImpl("Charles", "Lippman");

		Author author3= new AuthorImpl("David", "Piel");
		Author author4= new AuthorImpl("Pierre", "Chles");



		// code pour les livres

		CodeBook code005 = new CodeBookImpl("005", "Informatique");
		CodeBook code33 = new CodeBookImpl("33", "Science ");


		CodeBook code34 = new CodeBookImpl("34", "Droit");
		CodeBook code311 = new CodeBookImpl("311", "Statistiques");

		CodeBook code51 = new CodeBookImpl("51", "Mathematiques");
		CodeBook code53 = new CodeBookImpl("53", "Physique");

		CodeBook code89 = new CodeBookImpl("89", "Geographie");
		CodeBook code90 = new CodeBookImpl("90", "Histoire");

		




		// les livres

		Book book1 = new BookImpl("98777", "Geographie",author1,6,code33);
		Book book2 = new BookImpl("45566", "techniques de la geographie", author2, 2, code33);

		Book book3 = new BookImpl("66754", "droit public", author1,3,code34);
		Book book4 = new BookImpl("98765", "Physique quantique", author4,2,code53);


		BorrowedBook borrowedBook1 = new BorrowedBookImpl("345", "11/02/2011", "21/02/2011","21/02/2011", "Returned", book1);
		BorrowedBook borrowedBook2 = new BorrowedBookImpl("678", "12/02/2011", "24/02/2011","24/02/2011", "Returned", book2);

		BorrowedBook borrowedBook3 = new BorrowedBookImpl("889", "13/05/2011", "30/05/2011","", "No Returned", book3);
		BorrowedBook borrowedBook4 = new BorrowedBookImpl("990", "14/06/2011", "24/06/2011", "", "No Returned", book4);



		// adresse

		Address adress = new applications.libraryForm.fc.lib.AddressImpl("4 rue Albert 1er", "06200", "FRANCE", "0789908709");

		BorrowedBook[] borrowedBooks = new BorrowedBook[]{borrowedBook1,borrowedBook2,borrowedBook3,borrowedBook4};


		LibraryForm library = new LibraryFormImpl("Damas", "Jeanne", "07/09/1990", "Student",
				"30/06/2014", "UFR Lettres", "56789", "2508909", adress, borrowedBooks);



		return library;
	}


	@Override
	public String getStudentNumber() {
		// TODO Auto-generated method stub
		return this.studentNumber;
	}




	@Override
	public String[][] getReturnedBooks() {

		String[][] data = new String[4][10];

		LibraryForm library = getLibraryForm();

		if (library !=null)
		{
			BorrowedBook[] livresEmpruntes = library.getBorrowedBook();


			int m=0;
			for (int k = 0; k<livresEmpruntes.length; k++)
			{
				if(livresEmpruntes[k].getStatus().equals("Returned"))
				{
					String [] yearData = {
							livresEmpruntes[k].getLoanNumber(),
							livresEmpruntes[k].getBook().getTitle(),
							livresEmpruntes[k].getBook().getISBN(),
							livresEmpruntes[k].getBook().getAuthor().getFamilyName(),
							livresEmpruntes[k].getBook().getCodeBook().getCode(),
							livresEmpruntes[k].getBook().getCodeBook().getSignification(),
							livresEmpruntes[k].getDayOfIssue(),
							livresEmpruntes[k].getDayOfReturn(),
							livresEmpruntes[k].getEffectiveDayOfReturn(),
							livresEmpruntes[k].getStatus()

					};

					data[m++] = yearData;
				}
			}


			return data;
		}

		else{

			return null;
		}

	}
	
	
	@Override
	public String[][] getNonReturnedBooks() {

		String[][] data = new String[4][10];

		LibraryForm library = getLibraryForm();

		if (library !=null)
		{
			BorrowedBook[] livresEmpruntes = library.getBorrowedBook();


			int m=0;
			for (int k = 0; k<livresEmpruntes.length; k++)
			{
				if(livresEmpruntes[k].getStatus().equals("No Returned"))
				{
					String [] yearData = {
							livresEmpruntes[k].getLoanNumber(),
							livresEmpruntes[k].getBook().getTitle(),
							livresEmpruntes[k].getBook().getISBN(),
							livresEmpruntes[k].getBook().getAuthor().getFamilyName(),
							livresEmpruntes[k].getBook().getCodeBook().getCode(),
							livresEmpruntes[k].getBook().getCodeBook().getSignification(),
							livresEmpruntes[k].getDayOfIssue(),
							livresEmpruntes[k].getDayOfReturn(),
							livresEmpruntes[k].getEffectiveDayOfReturn(),
							livresEmpruntes[k].getStatus()

					};

					data[m++] = yearData;
				}
			}


			return data;
		}

		else{

			return null;
		}

	}


	@Override
	public String[] getSummaryTitles() {

		String[] summaryTitles = {"LoanNumber","Book's title", "ISBN","Author","Code book", "Domain name",
				"dayOfIssue","dayOfReturn","effective DayOfReturn","status"};
		return summaryTitles;
	}	
}
